package de.gympu.gympulan.gplserver.chatserver;

import java.util.Vector;

import de.gympu.gympulan.gplserver.GPLServerPluginProtocolOutput;
import de.gympu.gympulan.gplserver.plugins.gplserverplugin.GPLServerPluginConnection;

public class ChatServerConnection implements GPLServerPluginConnection {
	private static Vector<ChatServerConnection> connectedClients = new Vector<ChatServerConnection>();
	private ChatProtocol protocol;
	
	private ChatServerParser parser;
	
	private String name = "";
	
	private boolean loggedIn = false;

	public ChatServerConnection(ChatProtocol protocol) {
		this.protocol = protocol;
		
		connectedClients.add(this);
		
		this.parser = new ChatServerParser(this);
		
		protocol.giveName();
	}
	
	public void connectionClosed() {
		connectedClients.remove(this);
	}

	public void message(String[] data) {
		parser.parse(data);
	}

	public void sendMessageToUser(String string) {
		if(loggedIn) {
			for(int i = 0; i < connectedClients.size(); i++) {
				connectedClients.get(i).sendMessage(string);
			}
		}
	}
	
	public void sendMessage(String message) {
		if(loggedIn) {
			protocol.message(message);
		}
	}

	public void setName(String string) {
		if(!loggedIn) {
			this.name = string;
			this.loggedIn = true;
		}
	}

}
